$(document).ready(function () {
    var $form = $("#regForm");







    $form.bootstrapValidator({
        message: "不能为空",
        //ajax
        submitHandler: function (validator, form, submitButton) {
            //validator: 表单验证实例对象
            //form  jq对象  指定表单对象
            //submitButton  jq对象  指定提交按钮的对象
            // 实用ajax提交表单
            $.post(form.attr('action'), form.serialize(), function (result) {
                // .自定义回调逻辑
            }, 'json');
        },
        /**
         * 表单域配置
         */
        fields: {
            realname: {
                validators: {
                    notEmpty: {
                        message: '名字不能为空'
                    },
                    regexp: {
                        regexp: /^[\u4E00-\u9FA5]{2,4}$/,
                        message: '必须是汉字'
                    }
                }
            },
            tel: {
                validators: {
                    // stringLength: {
                    //     min: 11,
                    //     max: 11,
                    //     message: '用户名长度必须11位'
                    // },
                    notEmpty: {
                        message: '手机号不能为空'
                    },
                    regexp: {
                        regexp: /^1[34578]\d{9}$/,
                        message: '请输入正确的手机号'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    regexp: {
                        regexp: /^[0-9a-zA-Z]{4,16}/,
                        message: '必须是4-16位字母或数字组合'
                    }
                }
            },
            confirmPassword: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    identical: {
                        field: 'password',
                        message: '两次密码不一致'
                    }
                }
            },
            verification: {
                validators: {
                    notEmpty: {
                        message: '请输入验证码'
                    },

                    regexp: {
                        regexp: /^[0-9]{4}/,
                        message: '必须是4位数字'
                    }
                }
            }
        }

    });



    var start = true;
    var phStart = true;
    $("#ap").find('input').focus(function () {
        $('.addSpan').remove();
        phStart = true;
       
    });

    $(".v-code").click(function () {
        var now = 120;


        var that = this;
        var tel = $("[name=tel]").val();
        var url_s = $("#s-code").val();

        //判断是否填了手机号
        if (tel == '' || tel.length < 11 || !/^1[34578]\d{9}$/i.test(tel)) {

            //
            if (phStart) {
                var html = '<span class="addSpan">请输入手机号</span>'
                
                $("#ap").append(html);
            }
            phStart = false;

            start = false;
        } else {

            

            $.ajax({
                url: url_s,
                // url:'http://ip.taobao.com/service/getIpInfo.php?callback=jsoncallback&ip=58.23.36.245&_=1490147946818',
                type: 'post',
                async:false,
                data: {
                    tel: tel
                },
                success: function (data) {
                    if(data.code==1){
                        alert('发送成功！');
                    }else{
                        alert(data.msg);
                    }
                    // console.log(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log(textStatus);
                }
            })

            
        }


        if (start) {


            $(this).addClass("ck-cl");

            start = false;

            nowTime();

        }

        //倒计时
        function nowTime() {
            $(that).html(now + "秒后重新获取验证码");
            now--;
            if (now == 0) {
                clearTimeout(nowTime);
                $(that).html("获取验证码");
     
                $(that).removeClass("ck-cl");
                start = true;
            } else {
                setTimeout(nowTime, 1000);
            }
        }
    })


    $form.on('submit', function () {

        var flag = $(this).data('bootstrapValidator').isValid();
        var datas=$(this).serialize();
        var urls=$(this).attr("action");
        if (!flag) {
            alert("填写有误，请重新填写！");
        } else {
           
            $.ajax({
                type:'post',
                url: urls,
                async:false,
                data: datas,
                success: function (data) {
                    if(data.code==1){
                        alert(data.msg);
                        window.location.href="/";
                    }else{
                        alert(data.msg);
                    }
                    
                }
            })
        }

        return false;
    })





})